//
//  ChartInputDataCell.swift
//  HealthMonitoring
//
//  Created by YTYanK on 06/12/2018.
//  Copyright © 2018 YTYanK. All rights reserved.
//

import UIKit
import SwiftyJSON


class ChartInputDataCell: HMTableViewCell {
   
    var dateLabel = UILabel()
    var valueLabel = UILabel()
    var valueLabel1 = UILabel()
    var valueLabel2 = UILabel()
    var editButton = UIButton()
    var editBlock: ((_ button: UIButton, _ json: JSON) ->())?
    private  var  data: JSON?
    override func setupViews(view: UIView) {
        view.addSubview(dateLabel)
        view.addSubview(valueLabel)
        view.addSubview(valueLabel1)
        view.addSubview(valueLabel2)
        view.addSubview(editButton)
        
        
        let cellW: CGFloat = (view.wWidth - 40)/2;
        
        dateLabel.topAlign(view: view, predicate: "10")
        dateLabel.leadingAlign(view: view, predicate: "20")
//        dateLabel.text = "2018-12-15 09:28"
        dateLabel.heightConstrain(predicate: "34")
        dateLabel.widthConstrain(predicate: "\(cellW + 10)")

        valueLabel.topVertically(view: dateLabel, predicate: "0")
        valueLabel.leadingAlign(view: view, predicate: "20")
//        valueLabel.text = "体温：" + "38"
        valueLabel.textAlignment = .center;
        //valueLabel.backgroundColor = .green
        valueLabel.heightConstrain(predicate: "34")
        valueLabel.widthConstrain(predicate: "\(cellW - 10)")

        valueLabel1.topVertically(view: dateLabel, predicate: "0")
        valueLabel1.leadingHorizontally(view: valueLabel, predicate: "0")
        valueLabel1.textAlignment = .center;
        //valueLabel1.backgroundColor = .blue
        valueLabel1.heightConstrain(predicate: "34")
        valueLabel1.widthConstrain(predicate: "\(cellW - 10)")

        valueLabel2.topVertically(view: dateLabel, predicate: "0")
        valueLabel2.leadingHorizontally(view: valueLabel1, predicate: "0")
        valueLabel2.textAlignment = .center;
        valueLabel2.heightConstrain(predicate: "34")
        //valueLabel2.backgroundColor = .red
        valueLabel2.widthConstrain(predicate: "\(cellW - 10)")

        

        editButton.topAlign(view: view,predicate: "10")
        editButton.trailingAlign(view: view, predicate: "-20")
        editButton.setTitle("编辑", for: .normal)
        editButton.setTitleColor(configure.shareConfigure.HMColor1, for: .normal)
        editButton.addTarget(self, action: #selector(editClick), for: UIControlEvents.touchUpInside)
        editButton.heightConstrain(predicate: "34")
        editButton.widthConstrain(predicate: "50")
        
    }
    
    override func setJson(json: JSON) {
        self.data = json["data"]
       
        let time = json["data"]["recordTime"].stringValue
        let str =  String.chineseTime(frome: time)
        
        dateLabel.text =  String(str.prefix(16))
        
        let urlId = json["urlId"].stringValue
        //["blood-pressure-all", "weight-all", "body-temp-all","heart-rate-all","blood-glucose-all"]
        if urlId == "blood-pressure-all"  {
            valueLabel.text = "收缩压:" +  json["data"]["systolicPressure"].stringValue
            valueLabel1.text = "舒张压:" +  json["data"]["diastolicPressure"].stringValue
            valueLabel2.text = "心率:" +  json["data"]["heartRate"].stringValue
        }else if urlId == "weight-all" {
             valueLabel.text = "体重:" +  json["data"]["weight"].stringValue
            let type =  json["data"]["type"].intValue
            if type == 1 {
                valueLabel1.text = "餐前"
            }else if type == 2 {
                valueLabel1.text = "餐后"
            }else {
                valueLabel1.text = "---"
            }
            
        }else if urlId == "body-temp-all" {
            valueLabel.text = "体温:" +  json["data"]["temp"].stringValue
        }else if urlId == "heart-rate-all" {
            valueLabel.text = "心率:" +  json["data"]["heartRate"].stringValue
            let type =  json["data"]["type"].intValue
            if type == 1 {
                valueLabel1.text = "运动"
            }else if type == 2 {
                valueLabel1.text = "静息"
            }else {
                valueLabel1.text = "---"
            }
        }else if urlId == "blood-glucose-all" {
            valueLabel.text = "血糖:" +  json["data"]["bloodGlucose"].stringValue
        }

        //blood-pressure-all
    }
    
    
    @objc func editClick(sender: UIButton) {
//        print("=========>", sender, self.data!);
        self.editBlock?(sender, self.data!)
        
    }
}
